package com.pjl.restartlife.login.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pjl.restartlife.login.mapper.SysUserMapper;
import com.pjl.restartlife.login.service.SysUserService;
import com.pjl.restartlife.login.vo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
* @author jiao
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2024-04-03 16:50:38
*/
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public String login(SysUser sysUser) {
        try {
            SysUser user = sysUserMapper.getByUserNameSysUser(sysUser.getUsername());
            if(sysUser.getPassword().equals(user.getPassword())){
                log.info("用户："+sysUser.getUsername()+"=============密码正确,核对成功");
                return "succeed";
            }else {
                return "false，该用户未注册";
            }
        }catch (Exception exception){
            log.error(exception.getMessage());
            return "数据库错误";
        }
    }

    @Override
    public String register(SysUser sysUser) {
        try {
            SysUser user = sysUserMapper.getByUserNameSysUser(sysUser.getUsername());
            if (user==null) {
                sysUser.setId( UUID.randomUUID().toString().replaceAll("\\-",""));
                sysUserMapper.insertSysUser(sysUser);
                return "succeed";
            }else {
                return "false，该用户已存在，请登陆";
            }
        } catch (Exception exception) {
            log.error(exception.getMessage());
            exception.printStackTrace();
            return "数据库错误";
        }
    }

    @Override
    public List<SysUser> findAllUser() {
        List<SysUser> sysUserList = sysUserMapper.getALLSysUser();
        return sysUserList;
    }

    @Override
    public String loginPhone(SysUser sysUser) {
       SysUser user = sysUserMapper.getSysUserByPhone(sysUser.getPhone());
       if (user != null){
           log.info("用户："+sysUser.getUsername()+"=============密码正确,核对成功");
           return "succeed";
       }else {
           return "false,该用户未注册";
       }

    }
}




